package cn.liuhuan1.blogsModel.service;

import cn.liuhuan1.blogsModel.dao.FriendDao;
import cn.liuhuan1.entity.PageResult;
import cn.liuhuan1.entity.ReturnResult;
import cn.liuhuan1.webModelBean.api.FriendImp;
import cn.liuhuan1.webModelBean.bo.FriendBo;
import cn.liuhuan1.webModelBean.vo.FriendVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: liuhuan
 * @Date: 2021/2/22 - 02 - 22 - 16:02
 * @Description:
 * @version: 1.0
 */

@Service
public class FriendService implements FriendImp{
    @Autowired
    public FriendDao friendDao;

    @Override
    public List<FriendVo> getFriend(){
        List<FriendVo> friendVos = friendDao.getFriend();
        return friendVos;
    }

    @Override
    public PageResult<FriendVo> getPageFriend(FriendBo friendBo) {
        if(friendBo.getPageCurrent() < 1){
            friendBo.setPageCurrent(0);
        }else{
            friendBo.setPageCurrent(friendBo.getPageCurrent() - 1);
        }
        if(friendBo.getPageSize() < 9){
            friendBo.setPageCurrent(10);
        }

        PageResult<FriendVo> pageResult = new PageResult<>();

        List<FriendVo> friendVoList = friendDao.getPageFriend(friendBo);

        int count = friendDao.getPageCount(friendBo);

        pageResult.setPageList(friendVoList);

        pageResult.setPageShowTotal(count);
        pageResult.setPageCurrent(friendBo.getPageCurrent());
        pageResult.setPageSize(friendBo.getPageSize());
        return pageResult;
    }

    @Override
    public FriendVo getFriendById(String id) {
        if(id != null && id.trim() != ""){
            FriendVo friendVo = friendDao.getFriendById(id);
            if(friendVo != null){
                return friendVo;
            }
        }
        throw new NullPointerException();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveFriend(FriendBo friendBo) {

        friendDao.saveFriend(friendBo);
    }

    @Override
    public void updateFriend(FriendBo friendBo) {

        friendDao.updateFriend(friendBo);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delFriend(String id) {
        if(id != null && id.trim() != ""){
            friendDao.delFriend(id);
        }else{
            throw new NullPointerException();
        }
    }

    @Override
    public ReturnResult updateStatus(String id,Integer status) {
        if(null == id || id.trim() == ""){
            throw new NullPointerException("id不存在");
        }
        friendDao.updateStatus(id,status);
        return ReturnResult.succes();
    }


}
